#![allow(unused_imports,unused_assignments)]
use serde::{Deserialize, Serialize};
use sqlx::mysql::MySqlRow;
use sqlx::{FromRow, Row};
use sqlx::Error;
use chrono::{NaiveDate,NaiveDateTime};
use crate::domain::my_date_time_format;
use crate::domain::my_date_format;
use crate::domain::count_num::get_i64;
use crate::domain::count_num::get_i32;
use crate::domain::count_num::get_f64;
use crate::domain::count_num::get_f32;
use crate::domain::count_num::get_bool;
use crate::domain::count_num::get_string;
use crate::domain::count_num::get_dropdown;

use super::OrderItemBean;

#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct CustOrderBean {
	pub id: i64,
	pub name: String,
	pub active: bool,
	pub total: f64,
	pub amount: i32,
	pub status_id: i64,
	pub pay_type_id: i64,
	pub phone: String,
	pub address: String,
	#[serde(with = "my_date_time_format")]
	pub order_date_time: Option<NaiveDateTime>,
	pub user_id: i64,
}

#[derive(Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct CustOrderQueryRequest {
	pub id: Option<String>,
	pub name: Option<String>,
	pub active: Option<String>,
	pub total: Option<String>,
	pub amount: Option<String>,
	pub status_id: Option<String>,
	pub pay_type_id: Option<String>,
	pub phone: Option<String>,
	pub address: Option<String>,
	pub order_date_time: Option<String>,
	pub user_id: Option<String>,
	pub page:Option<i32>,
	pub rows:Option<i32>,
}

impl<'c> FromRow<'c, MySqlRow> for CustOrderBean {
	fn from_row(row: &MySqlRow) -> Result<Self, Error> {
		Ok(CustOrderBean {
			id: row.get(0),
			name: row.get(1),
			active: row.get(2),
			total:get_f64(row.get(3)),
			amount:get_i32(row.get(4)),
			status_id:get_dropdown(row.get(5)),
			pay_type_id:get_dropdown(row.get(6)),
			phone:get_string(row.get(7)),
			address:get_string(row.get(8)),
			order_date_time:row.get(9),
			user_id:get_dropdown(row.get(10)),
		})
	}
}

impl PartialEq for CustOrderBean {
	fn eq(&self, other: &Self) -> bool {
		self.id == other.id
	}
}
